<?php
/**
 * Order Dao definition 
 *
 * @author guzhi
 * @since 2009-04-21
 */
 
 require_once BaseUtil::getModulePath("base") . '/models/BaseDao.php';
 
 class OrderPackageDao extends BaseDao {
 	 protected $_name;

    public function  _setup() {
        $this->_name = $this->prefix() . "order_packages";
        parent::_setup();
    }
    
    public function addPackage($package){
    	return $this->insertData($package);
    }
    
    public function updatePackage($id, $package){
    	$where =  $this->db_w->quoteInto('id = ?', $id);
    	$this->updateData($package, $where);
    }
    
    public function deletePackage($id){
    	$isDel = array("is_delete"=>1);
    	$where =  $this->db_w->quoteInto('id = ?', $id);
    	$this->updateData($isDel, $where);
    }
    
    public function getPackages($orderId){
        $select = $this->db_r->select();
        $select->from($this->_name,"*");
        $select->where('order_id = ?', $orderId);
        $select->where("is_delete = 0");
        $select->order('tracking_num');
        $sql = $select->__toString();
        $result = $this->db_r->fetchAll($sql);
        return $result;
    }
    
    public function getCountById($id){
    	$select = $this->db_r->select();
    	$select->from($this->_name, "count(id) as num");
    	$select->where('order_id = ?', $id);
    	$select->where("is_delete = 0");
    	$sql = $select->__toString();
    	$num =  $this->db_r->fetchRow($sql);
    	return $num['num'];
    }
    
    public function countPackages(){
    	$select = $this->db_r->select();
    	$select->from($this->_name, array('count(id) as num', 'status'));
    	//$select->from($this->_name, array('count(id) as num', 'order_id', 'status'));
    	//$select->group('order_id');
    	$select->group('status');
    	$select->where('is_delete = 0');
    	$sql = $select->__toString();
    	return $this->db_r->fetchAll($sql);
    }
    
 }